package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.PriceResult;

import java.util.HashMap;
import java.util.Map;

public class MinePriceVM extends BaseViewModel<CommonRepository> {
    public PriceResult priceResult;

    public MinePriceVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onUIEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onPriceEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onMessagePriceCommand = new BindingCommand(() -> uc.onPriceEvent.setValue(1));
    public BindingCommand onVoicePriceCommand = new BindingCommand(() -> uc.onPriceEvent.setValue(2));
    public BindingCommand onVideoPriceCommand = new BindingCommand(() -> uc.onPriceEvent.setValue(3));
    public BindingCommand onVoiceStateCommand = new BindingCommand((o) -> requestState(1));
    public BindingCommand onVideoStateCommand = new BindingCommand((o) -> requestState(2));
    //=========================================接口：start===========================================

    /**
     * 收费价格列表
     */
    public void requestPriceInfo(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.priceInfo(map), this, new OnSuccessResult<PriceResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<PriceResult> response) {
                        priceResult = response.getData();
                        uc.onUIEvent.call();
                    }
                });
    }

    /**
     * 价格保存
     */
    public void requestPriceSave(int type, String price){
        Map<String, String> map = new HashMap<>();
        map.put("type", ""+type);
        map.put("price", price);
        HttpUtils.getInstance()
                .data(model.priceSave(map), this, new OnSuccessResult() {
                    @Override
                    public void onSuccessResult(BaseResponse response) {
                        requestPriceInfo();
                    }
                });
    }

    /**
     * 状态
     */
    public void requestState(int type){
        Map<String, String> map = new HashMap<>();
        map.put("type", ""+type);
        HttpUtils.getInstance()
                .data(model.priceState(map), this, new OnSuccessResult() {
                    @Override
                    public void onSuccessResult(BaseResponse response) {
                        requestPriceInfo();
                    }
                });
    }

}
